import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      component: () => import("@/views/Common.vue"),
      children: [
        {
          path: '/',
          name: 'Home',
          component: () => import("@/views/Home.vue"),
        },
        {
          path: '/Home',
          name: 'Home',
          component: () => import("@/views/Home.vue"),
        },
        {
          path: '/studentData',
          name: 'studentData',
          component: () => import("@/views/studentInfo/studentData.vue"),
        },
        {
          path: '/UploadTable',
          name: 'UploadTable',
          component: () => import("@/views/studentInfo/uploadTable.vue"),
        },
        {
          path: '/StuDataQuery',
          name: 'StuDataQuery',
          component: () => import("@/views/studentInfo/stuDataQuery.vue"),
        },
        {
          path: '/GraInfoManage',
          name: 'GraInfoManage',
          component: () => import("@/views/studentInfo/graInfoManage.vue"),
        },
        {
          path: '/Dept',
          name: 'Dept',
          component: () => import("@/views/studentInfo/remindStuOfDiff/dept.vue"),
        },
        {
          path: '/StuAffairs',
          name: 'StuAffairs',
          component: () => import("@/views/studentInfo/remindStuOfDiff/stuAffairs.vue"),
        },
        {
          path: '/FinanceOffice',
          name: 'FinanceOffice',
          component: () => import("@/views/studentInfo/remindStuOfDiff/financeOffice.vue"),
        },
        {
          path: '/DataCenter',
          name: 'DataCenter',
          component: () => import("@/views/studentInfo/remindStuOfDiff/dataCenter.vue"),
        },
        {
          path: '/SyncRecord',
          name: 'SyncRecord',
          component: () => import("@/views/studentInfo/syncRecord.vue"),
        },
        {
          path: '/StaffBasicInfo',
          name: 'StaffBasicInfo',
          component: () => import("@/views/basicStaffData/staffBasicInfo.vue"),
        },
        {
          path: '/DataQuery',
          name: 'DataQuery',
          component: () => import("@/views/basicStaffData/dataQuery.vue"),
        },
        {
          path: '/StaffSyncRecord',
          name: 'StaffSyncRecord',
          component: () => import("@/views/basicStaffData/staffSyncRecord.vue"),
        },
        {
          path: '/ResultsData',
          name: 'ResultsData',
          component: () => import("@/views/eduSystemData/resultsData.vue"),
        },
        {
          path: '/TeachingTaskData',
          name: 'TeachingTaskData',
          component: () => import("@/views/eduSystemData/teachingTaskData.vue"),
        },
        {
          path: '/EduDataQuery',
          name: 'EduDataQuery',
          component: () => import("@/views/eduSystemData/dataQuery.vue"),
        },
        {
          path: '/EduSyncRecord',
          name: 'EduSyncRecord',
          component: () => import("@/views/eduSystemData/syncRecord.vue"),
        },
        {
          path: '/BookInfo',
          name: 'BookInfo',
          component: () => import("@/views/librarySystemData/bookInfo.vue"),
        },
        {
          path: '/LibraryLendingdata',
          name: 'LibraryLendingdata',
          component: () => import("@/views/librarySystemData/libraryLendingdata.vue"),
        },
        {
          path: '/LibQueryData',
          name: 'LibQueryData',
          component: () => import("@/views/librarySystemData/libQueryData.vue"),
        },
        {
          path: '/LibSyncRecord',
          name: 'LibSyncRecord',
          component: () => import("@/views/librarySystemData/libSyncRecord.vue"),
        },
        {
          path: '/YXTConsumData',
          name: 'YXTConsumData',
          component: () => import("@/views/consumData/yxtConsumData.vue"),
        },
        {
          path: '/YXTQueryData',
          name: 'YXTQueryData',
          component: () => import("@/views/consumData/yxtQueryData.vue"),
        },
        {
          path: '/YXTSyncRecord',
          name: 'YXTSyncRecord',
          component: () => import("@/views/consumData/yxtSyncRecord.vue"),
        },
        {
          path: '/StuFeeInfo',
          name: 'StuFeeInfo',
          component: () => import("@/views/billingSystemData/stuFeeInfo.vue"),
        },
        {
          path: '/BillQueryData',
          name: 'BillQueryData',
          component: () => import("@/views/billingSystemData/billQueryData.vue"),
        },
        {
          path: '/BillSyncRecord',
          name: 'BillSyncRecord',
          component: () => import("@/views/billingSystemData/billSyncRecord.vue"),
        },
      ]
    }
  ]
})


router.beforeEach((to, from, next) => {
  var code = to.query.code;
  var token = localStorage.getItem('ACCESS_TOKEN')
  console.log(token, 'token');
  //  url token 和 缓存 token都存在的情况下 保存最新token到浏览器缓存
  if (code && token) {
    localStorage.setItem('ACCESS_TOKEN', JSON.stringify(code))
    next()
  }
  // //  只存在 url token
  if (code && !token) {
    localStorage.setItem('ACCESS_TOKEN', JSON.stringify(code))
    next()
  }
  // //  只存在本地缓存token
  if (!code && token) {
    next()
  }

  if (!code && !token) {
    var url = window.location.protocol + "//" + window.location.host;// 本地
    // window.location.href = url + '/gateway/dist/index.html#/login' //线上
    // window.location.href = url
    var tokenArr = window.location.search.split('=')
    console.log(tokenArr, 'tokenArr[1]');
    localStorage.setItem('ACCESS_TOKEN', tokenArr[1])
    next()
  }
})

export default router